/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quizzempire.model.statemachine;

import java.util.ArrayList;
import java.util.Random;
import quizzempire.model.IQuestion;
import quizzempire.model.Model;
import quizzempire.model.Question;
import quizzempire.model.Quizz;

/**
 *
 * @author Paulo
 */
public class AwaitsAnswer extends State {

    Quizz quizz;
    int difficulty, actual, correctas, total;
    ArrayList<Question> perguntas = new ArrayList<Question>();;
    boolean answered = false;
    
    public AwaitsAnswer(Model modelo, int difficulty) {
        super(modelo);
        quizz = modelo.getActiveQuizz();
        this.difficulty = difficulty;
        
        ArrayList<Question> quizzQ = quizz.getQuestions();
        Random rand = new Random();
        total = quizz.getNumQuestionsLevel()[difficulty];
        actual = 0;
        correctas = 0;
        
        while(perguntas.size() < total)
        {
            Question x = quizzQ.get(rand.nextInt(quizzQ.size()));
            if (x.getDifficulty() == difficulty && !perguntas.contains(x))
                perguntas.add(x);
        }
              
    }

    @Override
    public State setAnswer(int answerID) {
        
        if (answerID < 0 || answerID > 3)
            return this;
        
        if(perguntas.get(actual).getIndexCorrect() == answerID)
            correctas++;
        
        actual++;
        
        answered = true;
        
        return this;
    }
    
    @Override
    public State restart() {
        modelo.setChanges();
        return new AwaitsInstruction(modelo);
    }

    @Override
    public State nextQuestion() {
        
        if (!answered)
            return this;
        
        modelo.setChanges();
        
        if (actual < total)
            return this;
        
        return new ShowResults(modelo, difficulty, correctas);
    }
    
    
    @Override
    public IQuestion getQuestionViewer() {
        return perguntas.get(actual); 
    }

    @Override
    public int getDifficulty() {
        return difficulty;
    }
    
    
}

